package com.ruoyi.womp.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
	// 队列
	// 接收数据主队列
	public static final String MQ_QUEUE_REALTIME_DATA = "queue_realtime_data";

	public static final String MQ_QUEUE_ALARM_DATA = "queue_alarm_data";

	// 交换机
	// public static final String EXCHANGE_TOPIC_DC = "exchange_topic_dc";
	public static final String MQ_EXCHANGE_FANOUT_REALTIME_DATA = "exchange_fanout_realtime_data";

	public static final String MQ_EXCHANGE_FANOUT_ALARM_DATA = "exchange_fanout_alarm_data";

	// 交换机与队列绑定的RoutingKey
	// 接收数据主队列
	public static final String MQ_ROUTINGKEY_REALTIME_DATA = "routingkey_realtime_data";

	public static final String MQ_ROUTINGKEY_ALARM_DATA = "routingkey_alarm_data";
	// 数据中心 集中器TCP服务端返回命令下发结果队列
	// public static final String ROUTINGKEY_DC_CONCENTRATOR_COMMAND_RESULT =
	// "dc_concentrator_command_result";

	// 声明队列
	@Bean(MQ_QUEUE_REALTIME_DATA)
	public Queue MQ_QUEUE_REALTIME_DATA() {
		return new Queue(MQ_QUEUE_REALTIME_DATA);
	}

	@Bean(MQ_QUEUE_ALARM_DATA)
	public Queue MQ_QUEUE_ALARM_DATA() {
		return new Queue(MQ_QUEUE_ALARM_DATA);
	}

	// @Bean(QUEUE_DC_PUSH_LOCAL)
	// public Queue QUEUE_DC_PUSH_LOCAL() {
	// Map<String, Object> arguments = new HashMap<String, Object>();
	// arguments.put("x-message-ttl", 1000 * 60 * 60 * 24 * 7);
	// // arguments.put("x-message-ttl", 1000 * 30);
	// return
	// QueueBuilder.durable(QUEUE_DC_PUSH_LOCAL).withArguments(arguments).build();
	// }

	// 声明交换机
	// @Bean(EXCHANGE_TOPIC_DC)
	// public Exchange EXCHANGE_TOPIC_INFORM() {
	// // 声明了一个Topic类型的交换机，durable是持久化（重启rabbitmq这个交换机不会被自动删除）
	// return
	// ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_DC).durable(true).build();
	// }

	@Bean(MQ_EXCHANGE_FANOUT_REALTIME_DATA)
	public FanoutExchange MQ_EXCHANGE_FANOUT_REALTIME_DATA() {
		return new FanoutExchange(MQ_EXCHANGE_FANOUT_REALTIME_DATA);
	}

	@Bean(MQ_EXCHANGE_FANOUT_ALARM_DATA)
	public FanoutExchange MQ_EXCHANGE_FANOUT_ALARM_DATA() {
		return new FanoutExchange(MQ_EXCHANGE_FANOUT_ALARM_DATA);
	}

	// 声明队列和交换机绑定关系，并且指定RoutingKey
//	@Bean
//	public Binding DC_RECEIVER_DATA_MAIN_BINDING_TOPIC(@Qualifier(QUEUE_DC_RECEIVER_DATA_MAIN) Queue queue,
//			@Qualifier(EXCHANGE_TOPIC_DC) Exchange exchange) {
//		return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_DC_RECEIVER_DATA_MAIN).noargs();
//	}
//
//	@Bean
//	public Binding DC_WATER_BINDING_TOPIC(@Qualifier(QUEUE_DC_WATER_DATA_SAVE) Queue queue,
//			@Qualifier(EXCHANGE_TOPIC_DC) Exchange exchange) {
//		return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_DC_WATER_DATA_SAVE).noargs();
//	}
//
//	@Bean
//	public Binding DC_CONCENTRATOR_COMMAND_RESULT_BINDING_TOPIC(
//			@Qualifier(QUEUE_DC_CONCENTRATOR_COMMAND_RESULT) Queue queue,
//			@Qualifier(EXCHANGE_TOPIC_DC) Exchange exchange) {
//		return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_DC_CONCENTRATOR_COMMAND_RESULT).noargs();
//	}

	@Bean
	public Binding MQ_ROUTINGKEY_REALTIME_DATA(@Qualifier(MQ_QUEUE_REALTIME_DATA) Queue queue,
			@Qualifier(MQ_EXCHANGE_FANOUT_REALTIME_DATA) FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}

	@Bean
	public Binding MQ_ROUTINGKEY_ALARM_DATA(@Qualifier(MQ_QUEUE_ALARM_DATA) Queue queue,
			@Qualifier(MQ_EXCHANGE_FANOUT_ALARM_DATA) FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}
}